package com.ms.service.module.zone.db;

import java.util.ArrayList;

import com.ms.game.bank.db.CashLogModel;
import com.ms.game.db.dto.TransactionsDetailsUtil;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.account.db.AccountDAO;
import com.ms.service.module.zone.message.SCashGoldTransactionDetailsResponse;
import com.ms.service.module.zone.message.SSimpleCashLog;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class LoadCashGoldTransactionDetailsDCLAsync extends TaskAsyncCommand{


	public int messageType;
	public long startTime;
	public long endTime;
	public GUID playerID;
	public GUID sessionID;
	public GUID accountID;
	private ArrayList<CashLogModel> models;
	private ArrayList<SSimpleCashLog> simples;
	
	
	@Override
	public void asyncExecute() throws Exception {
		
		models = AccountDAO.getCashTransactionDetails(playerID.toString(),accountID.toString(),startTime, endTime);
		if (models != null && models.size() > 0) {
			//如果 models的size > 傳給client的長度限制 
			//i < 長度限制 ，  否則  i < model的size，
			simples = new ArrayList<SSimpleCashLog>();
			
			
			for (CashLogModel cashLogModel : models) {
				SSimpleCashLog log = TransactionsDetailsUtil.CashLogModleToSimple(cashLogModel);
				simples.add(log);
				
				if(simples.size() >=  TransactionsDetailsUtil.TRANSACTION_DETAIL_TO_CLIENT_LIMIT){
					break;
				}
			}
		}
	}

	@Override
	public void execute() throws Exception {
		SCashGoldTransactionDetailsResponse response = new SCashGoldTransactionDetailsResponse();
		if(models == null || models.size() <= 0){
			response.errorCode = ErrorCode.SUCCESS;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		response.transactionDetails = simples;
		//如果全部減掉送出去的 > 0 代表有剩下的資料
		response.hasDetailLeft = (models.size() - simples.size() > 0) ? true : false;
		response.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, messageType, response);
	}
}
